package demo.service.impl;
import com.richstonedt.garnet.model.bo.GroupBO;
import com.richstonedt.garnet.model.bo.RoleBO;
import com.richstonedt.garnet.model.bo.TenantBO;
import com.richstonedt.garnet.model.bo.UserBO;
import com.richstonedt.garnet.springboot.starter.config.GarnetProperties;
import demo.UserTypeEnum;
import demo.service.GarnetRpcService;
import demo.vo.SystemNormalUserSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GarnetRpcServiceImpl implements GarnetRpcService {

    private GarnetProperties garnetProperties;
    private RestTemplate restTemplate;
    private String registerUrl;

    @Autowired
    public GarnetRpcServiceImpl(RestTemplate restTemplate, GarnetProperties garnetProperties) {
        this.restTemplate = restTemplate;
        this.garnetProperties = garnetProperties;
        this.registerUrl = garnetProperties.getUrl() + "" +"";
    }

    public void callGarnetRegisterNormalUser(SystemNormalUserSaveVo saveVo) throws Exception {
        UserBO userBO = new UserBO();
        userBO.setPassword(saveVo.getPassword());
        userBO.setUsername(saveVo.getUsername());
        userBO.setPhone(saveVo.getPhone());
        userBO.setEmail(saveVo.getEmail());
        userBO.setType((byte) 1);
        userBO.setCreatedBy(1L);
        userBO.setStatus((byte) 1);

        //TODO:step2 权限相关
        Map hashMap  = new HashMap<>();
        hashMap.put("1", "306_306");
        hashMap.put("2", "305_308");
        hashMap.put("3", "312_312");
        hashMap.put("4", "307_305");
        hashMap.put("5", "313_313");
        hashMap.put("6", "314_314");
        hashMap.put("7", "315_315");
        hashMap.put("8", "304_304");

        String roleGroupStr = hashMap.get(saveVo.getType()).toString();
        setUserBO(roleGroupStr, userBO, hashMap, saveVo.getType());
    }

    private void setUserBO(String roleGroupStr, UserBO userBO, Map hashMap, String type) {
        //设置租户
        List<TenantBO> tenantBOS = new ArrayList<>();
        TenantBO tenantBO = new TenantBO();
        tenantBO.setId(322L);
        tenantBO.setName("rziomp");
        tenantBOS.add(tenantBO);
        userBO.setTenants(tenantBOS);
        //设置组
        List<GroupBO> groupBOS = new ArrayList<>();
        String[] roleGroup = roleGroupStr.split("_");
        GroupBO groupBO = new GroupBO();
        groupBO.setId(Long.parseLong(roleGroup[0]));
        groupBO.setName(UserTypeEnum.getDesc(type));
        groupBO.setType((byte) 1);
        groupBOS.add(groupBO);
        userBO.setGroups(groupBOS);
        //设置角色
        List<RoleBO> roleBOS = new ArrayList<>();
        RoleBO roleBO = new RoleBO();
        roleBO.setId(Long.parseLong(roleGroup[1]));
        roleBO.setName(UserTypeEnum.getDesc(type));
        roleBOS.add(roleBO);
        userBO.setRoles(roleBOS);
        //调用garnet接口
        restTemplate.put(registerUrl, userBO);
    }
}
